package com.can.Controller;


import com.can.domain.dto.LoginDto;
import com.can.domain.dto.RegisterDto;
import com.can.domain.vo.CommonVo;
import com.can.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;



@RestController
@RequestMapping("/user")
public class userontroller {

    @Autowired
    UserService userService;


    @PostMapping("/register")   //注册
    public CommonVo register(@RequestBody @Valid RegisterDto registerDto) {
        System.out.println("请求注册"+registerDto.toString());
        CommonVo res=userService.register( registerDto.getUsername(),  registerDto.getPassword());
//        return userService.register( registerDto.getUsername(),  registerDto.getPassword());
        System.out.println(res);
        return res;
    }


    @PostMapping("/login")       //登录

    public CommonVo login(@RequestBody LoginDto logindto) {
        System.out.println(logindto.toString());

        return userService.login(logindto.getUsername(), logindto.getPassword());

    }


    @GetMapping("/wode")       //我的
    public CommonVo wode(HttpServletRequest request) throws Exception {

        // 1.获取请求头中的用户信息
        String userId = request.getHeader("X-UserId") ;
        String power = request.getHeader("X-Power") ;
        System.out.print(userId);
        System.out.print(power);
        if(userId==null || power == null){
            System.out.println("没登陆，我的");
            return new CommonVo<>(400,"查询失败","");
        }
        return userService.wode(Integer.parseInt(userId));

    }

    @PostMapping("/jia3/{id}")
    public CommonVo jia3(@PathVariable(value ="id") int id) {
        return userService.jia3(id);
    }

}
